/*
 * @Author: your name
 * @Date: 2024-04-22 15:02:17
 * @LastEditTime: 2024-04-22 15:09:47
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\404.左叶子之和.cpp
 */
/*
 * @lc app=leetcode.cn id=404 lang=cpp
 *
 * [404] 左叶子之和
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int leftLeavesSum(TreeNode* root){
        int leftSum = 0;
        if(root == nullptr) return 0;
        if(root->left  && !root->left->left && !root->left->right){
            leftSum = root->left->val;
        }
        if(root->left){
            leftSum += leftLeavesSum(root->left);
        }
        if(root->right){
            leftSum += leftLeavesSum(root->right);
        }
        return leftSum;
    }
    int sumOfLeftLeaves(TreeNode* root) {
        return leftLeavesSum(root);
    }
};
// @lc code=end

